package com.zl.cms.service;

import com.zl.basic.CommonUtil.Check;
import com.zl.cms.dao.IRoleDao;
import com.zl.cms.dao.IUserDao;
import com.zl.cms.model.Role;
import com.zl.cms.model.User;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;

/**
 * User: zl email:c3tc3tc3t@126.com Date: 14-1-24 Time: 下午2:29
 */
@Service("roleService")
public class RoleService implements IRoleService {

  private IRoleDao roleDao;
  private IUserDao userDao;

  public IRoleDao getRoleDao() {
    return roleDao;
  }

  @Inject
  public void setRoleDao(IRoleDao roleDao) {
    this.roleDao = roleDao;
  }

  public IUserDao getUserDao() {
    return userDao;
  }

  @Inject
  public void setUserDao(IUserDao userDao) {
    this.userDao = userDao;
  }

  @Override
  public void add(Role role) {
    roleDao.add(role);
  }

  @Override
  public void delete(int id) {
    List<User> us = userDao.listUsersByRoleId(id);
    Check.checkIsNull(us, "删除的角色对象还有用户,扮演不能删除");
    roleDao.delete(id);
  }

  @Override
  public void update(Role role) {
    roleDao.update(role);
  }

  @Override
  public Role load(int id) {
    return roleDao.load(id);
  }

  @Override
  public List<Role> listRole() {
    return roleDao.listRole();
  }

  /**
   * 根据角色id删除对应的角色
   * 
   * @param rid 角色id
   */
  @Override
  public void deleteRoleUsers(int rid) {
    roleDao.deleteRoleUsers(rid);
  }
}
